﻿using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;

namespace HalconDotNet
{
	/// <summary>Represents an instance of a serializied item.</summary>
	// Token: 0x02000060 RID: 96
	public class HSerializedItem : HHandle
	{
		// Token: 0x0600177F RID: 6015 RVA: 0x0000883B File Offset: 0x00006A3B
		[EditorBrowsable(EditorBrowsableState.Never)]
		public HSerializedItem() : base(HHandleBase.UNDEF)
		{
		}

		// Token: 0x06001780 RID: 6016 RVA: 0x00096A80 File Offset: 0x00094C80
		[EditorBrowsable(EditorBrowsableState.Never)]
		public HSerializedItem(IntPtr handle) : base(handle)
		{
			this.AssertSemType();
		}

		// Token: 0x06001781 RID: 6017 RVA: 0x00096A8F File Offset: 0x00094C8F
		[EditorBrowsable(EditorBrowsableState.Never)]
		public HSerializedItem(HHandle handle) : base(handle)
		{
			this.AssertSemType();
		}

		// Token: 0x06001782 RID: 6018 RVA: 0x00096A9E File Offset: 0x00094C9E
		private void AssertSemType()
		{
			base.AssertSemType("serialized_item");
		}

		// Token: 0x06001783 RID: 6019 RVA: 0x00096AAB File Offset: 0x00094CAB
		internal static int LoadNew(IntPtr proc, int parIndex, int err, out HSerializedItem obj)
		{
			obj = new HSerializedItem(HHandleBase.UNDEF);
			return obj.Load(proc, parIndex, err);
		}

		// Token: 0x06001784 RID: 6020 RVA: 0x00096AC4 File Offset: 0x00094CC4
		internal static int LoadNew(IntPtr proc, int parIndex, int err, out HSerializedItem[] obj)
		{
			HTuple htuple;
			err = HTuple.LoadNew(proc, parIndex, err, out htuple);
			obj = new HSerializedItem[htuple.Length];
			for (int i = 0; i < htuple.Length; i++)
			{
				obj[i] = new HSerializedItem(HalconAPI.IsLegacyHandleMode() ? htuple[i].IP : htuple[i].H);
			}
			htuple.Dispose();
			return err;
		}

		/// <summary>
		///   Create a serialized item.
		///   Modified instance represents: Handle of the serialized item.
		/// </summary>
		/// <param name="pointer">Data pointer of the serialized item.</param>
		/// <param name="size">Size of the serialized item.</param>
		/// <param name="copy">Copy mode of the serialized item. Default: "true"</param>
		// Token: 0x06001785 RID: 6021 RVA: 0x00096B34 File Offset: 0x00094D34
		public HSerializedItem(IntPtr pointer, int size, string copy)
		{
			IntPtr proc = HalconAPI.PreCall(410);
			HalconAPI.StoreIP(proc, 0, pointer);
			HalconAPI.StoreI(proc, 1, size);
			HalconAPI.StoreS(proc, 2, copy);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			num = base.Load(proc, 0, num);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
		}

		/// <summary>Creates a new serialized item with data given in byte array</summary>
		/// <remarks>The array needs to be kept alive until block is disposed!</remarks>
		// Token: 0x06001786 RID: 6022 RVA: 0x00096B90 File Offset: 0x00094D90
		[EditorBrowsable(EditorBrowsableState.Never)]
		public HSerializedItem(byte[] data)
		{
			GCHandle gchandle = GCHandle.Alloc(data, GCHandleType.Pinned);
			IntPtr pointer = gchandle.AddrOfPinnedObject();
			this.CreateSerializedItemPtr(pointer, data.Length, "true");
			gchandle.Free();
		}

		/// <summary>Copies a serialized item into a new byte array</summary>
		// Token: 0x06001787 RID: 6023 RVA: 0x00096BCC File Offset: 0x00094DCC
		public static implicit operator byte[](HSerializedItem item)
		{
			int num;
			IntPtr serializedItemPtr = item.GetSerializedItemPtr(out num);
			byte[] array = new byte[num];
			Marshal.Copy(serializedItemPtr, array, 0, num);
			GC.KeepAlive(item);
			return array;
		}

		// Token: 0x06001788 RID: 6024 RVA: 0x00096BF8 File Offset: 0x00094DF8
		internal new void Serialize(Stream stream)
		{
			byte[] array = this;
			stream.Write(array, 0, array.Length);
		}

		// Token: 0x06001789 RID: 6025 RVA: 0x00096C18 File Offset: 0x00094E18
		internal new static HSerializedItem Deserialize(Stream stream)
		{
			BinaryReader binaryReader = new BinaryReader(stream);
			byte[] array = binaryReader.ReadBytes(16);
			ulong num;
			if (array.Length < 16 || HalconAPI.IsFailure(HalconAPI.GetSerializedSize(array, out num)))
			{
				throw new HalconException("Input stream is no serialized HALCON object");
			}
			//if (num > (ulong)-1879049217)
			if (num > (ulong)0x8FFFFBFF)
			{
				throw new HalconException("Input stream too large");
			}
			byte[] array2 = binaryReader.ReadBytes((int)num);
			if (array2.Length < (int)num || HalconAPI.IsFailure(HalconAPI.GetSerializedSize(array, out num)))
			{
				throw new HalconException("Unexpected end of serialization data");
			}
			byte[] array3 = new byte[(int)num + 16];
			array.CopyTo(array3, 0);
			array2.CopyTo(array3, 16);
			return new HSerializedItem(array3);
		}

		/// <summary>
		///   Receive a serialized item over a socket connection.
		///   Modified instance represents: Handle of the serialized item.
		/// </summary>
		/// <param name="socket">Socket number.</param>
		// Token: 0x0600178A RID: 6026 RVA: 0x00096CB4 File Offset: 0x00094EB4
		public void ReceiveSerializedItem(HSocket socket)
		{
			this.Dispose();
			IntPtr proc = HalconAPI.PreCall(403);
			HalconAPI.Store(proc, 0, socket);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			num = base.Load(proc, 0, num);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			GC.KeepAlive(socket);
		}

		/// <summary>
		///   Send a serialized item over a socket connection.
		///   Instance represents: Handle of the serialized item.
		/// </summary>
		/// <param name="socket">Socket number.</param>
		// Token: 0x0600178B RID: 6027 RVA: 0x00096D08 File Offset: 0x00094F08
		public void SendSerializedItem(HSocket socket)
		{
			IntPtr proc = HalconAPI.PreCall(404);
			base.Store(proc, 1);
			HalconAPI.Store(proc, 0, socket);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(this);
			GC.KeepAlive(socket);
		}

		/// <summary>
		///   Write a serialized item to a file.
		///   Instance represents: Handle of the serialized item.
		/// </summary>
		/// <param name="fileHandle">File handle.</param>
		// Token: 0x0600178C RID: 6028 RVA: 0x00096D4C File Offset: 0x00094F4C
		public void FwriteSerializedItem(HFile fileHandle)
		{
			IntPtr proc = HalconAPI.PreCall(405);
			base.Store(proc, 1);
			HalconAPI.Store(proc, 0, fileHandle);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(this);
			GC.KeepAlive(fileHandle);
		}

		/// <summary>
		///   Read a serialized item from a file.
		///   Modified instance represents: Handle of the serialized item.
		/// </summary>
		/// <param name="fileHandle">File handle.</param>
		// Token: 0x0600178D RID: 6029 RVA: 0x00096D90 File Offset: 0x00094F90
		public void FreadSerializedItem(HFile fileHandle)
		{
			this.Dispose();
			IntPtr proc = HalconAPI.PreCall(406);
			HalconAPI.Store(proc, 0, fileHandle);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			num = base.Load(proc, 0, num);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			GC.KeepAlive(fileHandle);
		}

		/// <summary>Delete a serialized item.</summary>
		/// <param name="serializedItemHandle">Handle of the serialized item.</param>
		// Token: 0x0600178E RID: 6030 RVA: 0x00096DE4 File Offset: 0x00094FE4
		public static void ClearSerializedItem(HSerializedItem[] serializedItemHandle)
		{
			HTuple htuple = HHandleBase.ConcatArray(serializedItemHandle);
			IntPtr proc = HalconAPI.PreCall(408);
			HalconAPI.Store(proc, 0, htuple);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.UnpinTuple(htuple);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(serializedItemHandle);
		}

		/// <summary>
		///   Delete a serialized item.
		///   Instance represents: Handle of the serialized item.
		/// </summary>
		// Token: 0x0600178F RID: 6031 RVA: 0x00096E28 File Offset: 0x00095028
		public void ClearSerializedItem()
		{
			IntPtr proc = HalconAPI.PreCall(408);
			base.Store(proc, 0);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Access the data pointer of a serialized item.
		///   Instance represents: Handle of the serialized item.
		/// </summary>
		/// <param name="size">Size of the serialized item.</param>
		/// <returns>Data pointer of the serialized item.</returns>
		// Token: 0x06001790 RID: 6032 RVA: 0x00096E5C File Offset: 0x0009505C
		public IntPtr GetSerializedItemPtr(out int size)
		{
			IntPtr proc = HalconAPI.PreCall(409);
			base.Store(proc, 0);
			HalconAPI.InitOCT(proc, 0);
			HalconAPI.InitOCT(proc, 1);
			int num = HalconAPI.CallProcedure(proc);
			IntPtr result;
			num = HalconAPI.LoadIP(proc, 0, num, out result);
			num = HalconAPI.LoadI(proc, 1, num, out size);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			return result;
		}

		/// <summary>
		///   Create a serialized item.
		///   Modified instance represents: Handle of the serialized item.
		/// </summary>
		/// <param name="pointer">Data pointer of the serialized item.</param>
		/// <param name="size">Size of the serialized item.</param>
		/// <param name="copy">Copy mode of the serialized item. Default: "true"</param>
		// Token: 0x06001791 RID: 6033 RVA: 0x00096EB8 File Offset: 0x000950B8
		public void CreateSerializedItemPtr(IntPtr pointer, int size, string copy)
		{
			this.Dispose();
			IntPtr proc = HalconAPI.PreCall(410);
			HalconAPI.StoreIP(proc, 0, pointer);
			HalconAPI.StoreI(proc, 1, size);
			HalconAPI.StoreS(proc, 2, copy);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			num = base.Load(proc, 0, num);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
		}
	}
}
